<?php
/**
 * Created by PhpStorm.
 * User: sunzexin
 * Date: 2018/1/8
 * Time: 16:36
 */

namespace App\Http\Logics\MiniProgram;


use App\Auth;
use App\MenuMappingAuth;
use App\Http\Logics\BaseLogic;
class AuthLogic extends BaseLogic
{
    public static function getAuthList($data)
    {
        $query = new Auth();
        $query = $query->append(['type_text'])->where(function ($query) use ($data){
            if ($data['type'] != ''){
                $query->where('type', $data['type']);
            }
            if (!empty($data['name'])){
                $query->where('name', 'like', '%' . $data['name'] . '%');
            }
        })->orderBy('id', 'desc');
        $auth = $data['limit'] ? $query->paginate($data['limit']) : $query->get();
        $auth->map(function ($auth) {
            return $auth->append('type_text');
        });
        if (empty($auth)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        return self::response(self::SUCCESS, $auth);
    }

    public static function createAuth($data)
    {
        $auth = new Auth();
        $auth->name = $data['name'];
        $auth->route = $data['route'];
        $auth->type = $data['type'];
        if (!$auth->save()){
            return self::response(self::OBJECT_SAVE_FAIL);
        }
        return self::response(self::SUCCESS);
    }

    public static function deleteAuth($data)
    {
        $auth = Auth::find($data['id']);
        if (empty($auth)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        if (!$auth->delete()){
            return self::response(self::OBJECT_DELETE_FAILD);
        }
        return self::response(self::SUCCESS);
    }

    public static function updateAuth($data)
    {
        $auth = Auth::find($data['id']);
        if (empty($auth)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        $auth->name = $data['name'];
        $auth->route = $data['route'];
        $auth->type = $data['type'];
        if (!$auth->save()){
            return self::response(self::OBJECT_SAVE_FAIL);
        }
        return self::response(self::SUCCESS, $auth);
    }
}